home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 23 / AACD 23.iso / AACD / Programming / Wipeout / source / nametag.c < prev    next >
C/C++ Source or Header  |  2000-12-18  |  4KB  |  155 lines

  1. /*
  2.  * $Id: nametag.c 1.2 2000/12/18 16:23:05 olsen Exp olsen $
  3.  *
  4.  * :ts=4
  5.  *
  6.  * Wipeout -- Traces and munges memory and detects memory trashing
  7.  *
  8.  * Written by Olaf `Olsen' Barthel <olsen@sourcery.han.de>
  9.  * Public Domain
  10.  */
  11.  
  12. #ifndef _GLOBAL_H
  13. #include "global.h"
  14. #endif    /* _GLOBAL_H */
  15.  
  16. /******************************************************************************/
  17.  
  18. struct NameTag
  19. {
  20.     ULONG    nt_Checksum;        /* checksum for entire data structure */
  21.     ULONG    nt_Size;            /* size of entire data structure */
  22.     ULONG    nt_Segment;            /* hunk number allocation came from */
  23.     ULONG    nt_Offset;            /* hunk offset allocation came from */
  24.     LONG    nt_ProgramNameLen;    /* name of program making the call */
  25.     LONG    nt_TaskNameLen;        /* name of task/process making the call */
  26.     UBYTE    nt_Names[2];        /* both program and task name */
  27. };
  28.  
  29. /******************************************************************************/
  30.  
  31. STATIC ULONG LocalSegment;
  32. STATIC ULONG LocalOffset;
  33. STATIC UBYTE LocalProgramNameBuffer[MAX_FILENAME_LEN];
  34. STATIC UBYTE LocalTaskNameBuffer[MAX_FILENAME_LEN];
  35.  
  36. /******************************************************************************/
  37.  
  38. LONG
  39. GetNameTagLen(ULONG pc)
  40. {
  41.     LONG bytes = 0;
  42.  
  43.     /* determine the name and the origin of the caller */
  44.     if(GetTaskName(NULL,LocalTaskNameBuffer,sizeof(LocalTaskNameBuffer)))
  45.     {
  46.         if(FindAddress(pc,sizeof(LocalProgramNameBuffer),LocalProgramNameBuffer,&LocalSegment,&LocalOffset))
  47.         {
  48.             /* calculate the size of the data structure to allocate later */
  49.             bytes = ((sizeof(struct NameTag) + strlen(LocalTaskNameBuffer) + strlen(LocalProgramNameBuffer)) + MEM_BLOCKMASK) & ~MEM_BLOCKMASK;
  50.         }
  51.     }
  52.  
  53.     return(bytes);
  54. }
  55.  
  56. /******************************************************************************/
  57.  
  58. STATIC struct NameTag *
  59. GetNameTag(const APTR mem,LONG size)
  60. {
  61.     struct NameTag * nt = NULL;
  62.  
  63.     ASSERT(th != NULL);
  64.  
  65.     /* return the nametag corresponding to the given memory area */
  66.     if(size > 0)
  67.         nt = (struct NameTag *)(((ULONG)mem) - size);
  68.  
  69.     return(nt);
  70. }
  71.  
  72. /******************************************************************************/
  73.  
  74. VOID
  75. FillNameTag(APTR mem,LONG size)
  76. {
  77.     struct NameTag * nt;
  78.     STRPTR name;
  79.  
  80.     ASSERT(mem != NULL && size > 0);
  81.  
  82.     nt = (struct NameTag *)mem;
  83.  
  84.     /* fill in the name tag header with data */
  85.     nt->nt_Size                = size;
  86.     nt->nt_Segment            = LocalSegment;
  87.     nt->nt_Offset            = LocalOffset;
  88.     nt->nt_ProgramNameLen    = strlen(LocalProgramNameBuffer)+1;
  89.     nt->nt_TaskNameLen        = strlen(LocalTaskNameBuffer)+1;
  90.  
  91.     /* fill in the names */    
  92.     name = nt->nt_Names;
  93.     strcpy(name,LocalProgramNameBuffer);
  94.     
  95.     name += strlen(LocalProgramNameBuffer)+1;
  96.     strcpy(name,LocalTaskNameBuffer);
  97.  
  98.     /* fix the checksum */
  99.     nt->nt_Checksum    = 0;
  100.     nt->nt_Checksum = ~CalculateChecksum((ULONG *)nt,nt->nt_Size);
  101. }
  102.  
  103. /******************************************************************************/
  104.  
  105. BOOL
  106. GetNameTagData(
  107.     const APTR    mem,
  108.     LONG        size,
  109.     STRPTR *    programNamePtr,
  110.     ULONG *        segmentPtr,
  111.     ULONG *        offsetPtr,
  112.     STRPTR *    taskNamePtr)
  113. {
  114.     struct NameTag * nt;
  115.     BOOL found = FALSE;
  116.  
  117.     ASSERT(th != NULL);
  118.  
  119.     /* if a name tag header corresponds to the memory
  120.      * chunk, extract its data
  121.      */
  122.     nt = GetNameTag(mem,size);
  123.     if(nt != NULL)
  124.     {
  125.         /* is the data still intact? */
  126.         if(CalculateChecksum((ULONG *)nt,nt->nt_Size) == (ULONG)-1)
  127.         {
  128.             STRPTR name;
  129.  
  130.             /* fill in the data */
  131.  
  132.             name = nt->nt_Names;
  133.  
  134.             if(programNamePtr != NULL)
  135.                 (*programNamePtr) = name;
  136.  
  137.             if(segmentPtr != NULL)
  138.                 (*segmentPtr) = nt->nt_Segment;
  139.  
  140.             if(offsetPtr != NULL)
  141.                 (*offsetPtr) = nt->nt_Offset;
  142.  
  143.             if(taskNamePtr != NULL)
  144.             {
  145.                 name += nt->nt_ProgramNameLen;
  146.                 (*taskNamePtr) = name;
  147.             }
  148.  
  149.             found = TRUE;
  150.         }
  151.     }
  152.  
  153.     return(found);
  154. }
  155.